<template>
  <div class="create-update-space">
    <el-form :model="spaceInfo" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="广告位名称" prop='name'>
        <el-input v-model="spaceInfo.name"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang='ts'>
import { Form } from 'element-ui'
import Vue from 'vue'
import { saveOrUpdateSpace, getSpaceById } from '@/services/advertise'
export default Vue.extend({
  name: 'CreateOrUpdateSpace',
  data () {
    return {
      spaceInfo: {
        name: '',
        id: ''
      }
    }
  },
  created () {
    const { id } = this.$route.query
    if (id) {
      this.spaceInfo.id = id as string
      this.loadSpace()
    }
  },
  methods: {
    async loadSpace () {
      const { data } = await getSpaceById({
        id: this.spaceInfo.id
      })
      this.spaceInfo = data.content
    },
    async submitForm () {
      await saveOrUpdateSpace(this.spaceInfo)
      this.$message.success('操作成功')
      this.$router.back()
    },
    resetForm (formName: string) {
      (this.$refs[formName] as Form).resetFields()
    }
  }
})
</script>

<style>

</style>
